Imports System.Text
Imports System.Web.UI.WebControls
Imports System.Collections.Specialized

Namespace DotNetNuke.Modules.ActiveForums
    Partial Public Class admin_manageforums_forumeditor
        Inherits ActiveAdminBase
        Public imgOn As String
        Public imgOff As String
        Public editorType As String = "G" '"F"
        Public recordId As Integer = 0
        Protected WithEvents ctlSecurityGrid As New Controls.admin_securitygrid
#Region "Event Handlers"
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load


            cbMaint.LoadingTemplate = GetLoadingTemplateSmall()
            litTopicPropButton.Text = "<div><a href="""" onclick=""afadmin_LoadPropForm();return false;"" class=""btnadd afroundall"">[RESX:AddProperty]</a></div>"
            litPropLoad.Text = Utilities.GetFileResource("afadmin.properties.js")


            BindRoles()
            Dim sepChar As String = "|"
            If Not Params Is Nothing AndAlso Not String.IsNullOrEmpty(Params) Then
                If Params.Contains("!") Then
                    sepChar = "!"
                End If
                editorType = Params.Split(CChar(sepChar))(1) ' Params.Split(CChar(sepChar))(1).Split(CChar("="))(1)
                recordId = CInt(Params.Split(CChar(sepChar))(0))
            End If
            If editorType = "G" Then
                trGroups.Visible = False
                reqGroups.Enabled = False
                trDesc.Visible = False
                trInherit.Visible = False
                lblForumGroupName.Text = GetSharedResource("[RESX:GroupName]")
                btnDelete.ClientSideScript = "deleteGroup();"

            Else
                lblForumGroupName.Text = GetSharedResource("[RESX:ForumName]")
                trInherit.Visible = True
                chkInheritGroup.Attributes.Add("onclick", "amaf_toggleInherit();")
                btnDelete.ClientSideScript = "deleteForum();"
            End If
            If recordId = 0 Then
                btnDelete.Visible = False
            End If
            imgOn = Page.ResolveUrl("~/DesktopModules/ActiveForums/images/admin_check.png")
            imgOff = Page.ResolveUrl("~/DesktopModules/ActiveForums/images/admin_stop.png")
            reqForumName.Text = "<img src=""" & Page.ResolveUrl(RequiredImage) & """ />"
            reqGroups.Text = "<img src=""" & Page.ResolveUrl(RequiredImage) & """ />"
            Dim propImage As String = "<img src=""" & Page.ResolveUrl("~/DesktopModules/ActiveForums/images/properties16.png") & """ alt=""[RESX:ConfigureProperties]"" />"

            rdAttachOn.Attributes.Add("onclick", "toggleAttach(this);")
            rdAttachOn.Attributes.Add("value", "1")
            rdAttachOff.Attributes.Add("onclick", "toggleAttach(this);")
            rdAttachOff.Attributes.Add("value", "0")
          
            rdHTMLOn.Attributes.Add("onclick", "toggleEditor(this);")
            rdHTMLOff.Attributes.Add("onclick", "toggleEditor(this);")
            rdHTMLOn.Attributes.Add("value", "1")
            rdHTMLOff.Attributes.Add("value", "0")
            cfgHTML.Attributes.Add("style", "display:none;")
            cfgHTML.InnerHtml = propImage
            rdModOn.Attributes.Add("onclick", "toggleMod(this);")
            rdModOff.Attributes.Add("onclick", "toggleMod(this);")
            rdModOn.Attributes.Add("value", "1")
            rdModOff.Attributes.Add("value", "0")
            cfgMod.Attributes.Add("style", "display:none;")
            cfgMod.InnerHtml = propImage



            trAutoSub.Visible = True
            rdAutoSubOn.Attributes.Add("onclick", "toggleAutoSub(this);")
            rdAutoSubOff.Attributes.Add("onclick", "toggleAutoSub(this);")
            rdAutoSubOn.Attributes.Add("value", "1")
            rdAutoSubOff.Attributes.Add("value", "0")
            cfgAutoSub.Attributes.Add("style", "display:none;")
            cfgAutoSub.InnerHtml = propImage
            txtOlderThan.Attributes.Add("onkeypress", "return onlyNumbers(event)")
            txtReplyOlderThan.Attributes.Add("onkeypress", "return onlyNumbers(event)")
            txtUserId.Attributes.Add("onkeypress", "return onlyNumbers(event)")
            If MainSettings.DeleteBehavior = 1 Then
                lblMaintWarn.Text = String.Format(GetSharedResource("[RESX:MaintenanceWarning]"), GetSharedResource("[RESX:MaintenanceWarning:Recycle]"), GetSharedResource("[RESX:MaintenanceWarning:Recycle:Desc]"))
            Else
                lblMaintWarn.Text = String.Format(GetSharedResource("[RESX:MaintenanceWarning]"), GetSharedResource("[RESX:MaintenanceWarning:Remove]"), GetSharedResource("[RESX:MaintenanceWarning:Remove:Desc]"))
            End If



            drpEditorTypes.Attributes.Add("onchange", "toggleEditorFields();")
            If Not cbEditorAction.IsCallback Then
                BindGroups()
                BindTemplates()
                If recordId > 0 Then
                    If editorType = "F" Then
                        LoadForum(recordId)
                    ElseIf editorType = "G" Then
                        LoadGroup(recordId)
                    End If
                    cfgHTML.Attributes.Add("onclick", "showProp(this,'edProp')")

                    cfgMod.Attributes.Add("onclick", "showProp(this,'modProp')")

                    cfgAutoSub.Attributes.Add("onclick", "showProp(this,'subProp')")



                    Dim sb As New Text.StringBuilder
                    sb.Append("<script type=""text/javascript"">")
                    sb.Append("function toggleAutoSub(obj){")
                    sb.Append("    closeAllProp();")
                    sb.Append("    var mod = document.getElementById('" & cfgAutoSub.ClientID & "');")
                    sb.Append("    if (obj.value == '1'){")
                    sb.Append("        mod.style.display = '';")
                    sb.Append("    }else{")
                    sb.Append("        mod.style.display = 'none';")
                    sb.Append("        var winDiv = document.getElementById('subProp');")
                    sb.Append("        mod.style.display = 'none';")
                    sb.Append("    };")
                    sb.Append("};")

                    sb.Append("</script>")
                    litScripts.Text = sb.ToString


                End If
                BindTabs()
            End If



        End Sub
        Private Sub cbEditorAction_Callback(ByVal sender As Object, ByVal e As Modules.ActiveForums.Controls.CallBackEventArgs) Handles cbEditorAction.Callback

            Select Case e.Parameters(0).ToLowerInvariant
                Case "forumsave"
                    Dim fi As New Forum
                    Dim fc As New ForumController
                    Dim bIsNew As Boolean = False
                    Dim forumId As Integer = 0
                    Dim ForumGroupId As Integer = 0
                    Dim ForumSettingsKey As String = String.Empty

                    If CInt(e.Parameters(1)) <= 0 Then
                        bIsNew = True
                        fi.ForumID = -1
                    Else
                        fi = New Forum
                        fi = fc.Forums_Get(PortalId, ModuleId, CInt(e.Parameters(1)), Me.UserId, False, False, -1)
                        ForumSettingsKey = fi.ForumSettingsKey
                    End If
                    With fi
                        '.ForumID = CInt(e.Parameters(1))
                        'If .ForumID = 0 Then
                        '    bIsNew = True
                        'End If
                        .ModuleId = ModuleId
                        .PortalId = PortalId
                        Dim sParentValue As String = ""
                        sParentValue = e.Parameters(2).ToString
                        Dim ParentForumId As Integer = 0

                        If InStr(sParentValue, "GROUP") > 0 Then
                            sParentValue = Replace(sParentValue, "GROUP", "")
                            ForumGroupId = CType(sParentValue, Integer)
                        Else
                            ParentForumId = CType(Replace(sParentValue, "FORUM", ""), Integer)
                            ForumGroupId = fc.Forums_GetGroupId(ParentForumId)
                        End If
                        .ForumGroupId = ForumGroupId
                        .ParentForumId = ParentForumId
                        .ForumName = e.Parameters(3).ToString
                        .ForumDesc = e.Parameters(4).ToString
                        .Active = CBool(e.Parameters(5))
                        .Hidden = CBool(e.Parameters(6))
                        If e.Parameters(7).ToString = "" Then
                            .SortOrder = 0
                        Else
                            .SortOrder = CInt(e.Parameters(7))
                        End If
                        Dim fkey As String = String.Empty
                        If Not String.IsNullOrEmpty(.ForumSettingsKey) Then
                            fkey = .ForumSettingsKey
                        End If
                        If CBool(e.Parameters(8)) Then
                            Dim fgc As New ForumGroupController
                            Dim fgi As ForumGroupInfo = fgc.GetForumGroup(ModuleId, ForumGroupId)
                            If bIsNew Then
                                .PermissionsId = fgi.PermissionsId

                            Else
                                If Not .ForumSettingsKey = "G:" & ForumGroupId.ToString Then
                                    .PermissionsId = fgi.PermissionsId
                                End If
                            End If
                            .ForumSettingsKey = "G:" & ForumGroupId.ToString

                        ElseIf bIsNew Or fkey.Contains("G:") Then
                            .ForumSettingsKey = ""
                            If .InheritSecurity Then
                                .PermissionsId = -1
                            End If
                        Else
                            .ForumSettingsKey = "F:" & fi.ForumID.ToString()
                        End If
                        If ForumSettingsKey <> fkey And fkey.Contains("F:") Then
                            bIsNew = True
                        End If
                        .PrefixURL = e.Parameters(9)
                        If Not String.IsNullOrEmpty(.PrefixURL) Then
                            Dim db As New Data.Common
                            If Not db.CheckForumURL(PortalId, ModuleId, .PrefixURL, .ForumID, .ForumGroupId) Then
                                .PrefixURL = String.Empty
                            End If
                        End If
                    End With

                    forumId = fc.Forums_Save(PortalId, fi, bIsNew, CBool(e.Parameters(8)))
                    recordId = forumId
                    Dim securityKey As String = String.Empty
                    DataCache.ClearForumGroupsCache(ModuleId)
                    Dim cachekey As String = String.Format("AF-FI-{0}-{1}-{2}", PortalId, ModuleId, forumId)
                    DataCache.CacheClear(cachekey)
                    hidEditorResult.Value = forumId.ToString
                Case "groupsave"
                    Dim bIsNew As Boolean = False
                    Dim gi As New ForumGroupInfo
                    Dim fgc As New ForumGroupController
                    Dim groupId As Integer = CInt(e.Parameters(1))
                    If groupId > 0 Then
                        gi = fgc.Groups_Get(ModuleId, groupId)
                    End If
                    Dim securityKey As String = String.Empty
                    If groupId = 0 Then
                        bIsNew = True
                    Else
                        securityKey = "G:" & groupId
                    End If
                    gi.ModuleId = ModuleId
                    gi.ForumGroupId = groupId
                    gi.GroupName = e.Parameters(3).ToString
                    gi.Active = CBool(e.Parameters(5))
                    gi.Hidden = CBool(e.Parameters(6))
                    If e.Parameters(7).ToString = "" Then
                        gi.SortOrder = 0
                    Else
                        gi.SortOrder = CInt(e.Parameters(7))
                    End If
                    gi.PrefixURL = e.Parameters(9)
                    If Not String.IsNullOrEmpty(gi.PrefixURL) Then
                        Dim db As New Data.Common
                        If Not db.CheckGroupURL(PortalId, ModuleId, gi.PrefixURL, gi.ForumGroupId) Then
                            gi.PrefixURL = String.Empty
                        End If
                    End If
                    gi.GroupSettingsKey = securityKey
                    Dim gc As New ForumGroupController
                    groupId = gc.Groups_Save(PortalId, gi, bIsNew)
                    recordId = groupId

                    DataCache.ClearForumGroupsCache(ModuleId)
                    hidEditorResult.Value = groupId.ToString


                Case "forumsettingssave"
                    Dim ForumId As Integer = CInt(e.Parameters(1))
                    Dim sKey As String = "F:" & ForumId.ToString
                    SaveSettings(sKey, e.Parameters)

                    hidEditorResult.Value = ForumId.ToString
                    DataCache.CacheClear(ForumId.ToString & "ForumSettings")
                    DataCache.CacheClear(String.Format(CacheKeys.ForumInfo, ForumId))
                    DataCache.CacheClear(String.Format(CacheKeys.ForumInfo, ForumId) & "st")
                    Dim cachekey As String = String.Format("AF-FI-{0}-{1}-{2}", PortalId, ModuleId, ForumId)
                    DataCache.CacheClear(cachekey)
                Case "groupsettingssave"
                    Dim ForumId As Integer = CInt(e.Parameters(1))
                    Dim sKey As String = "G:" & ForumId.ToString
                    SaveSettings(sKey, e.Parameters)

                    hidEditorResult.Value = ForumId.ToString
                    DataCache.CacheClear(ForumId.ToString & "GroupSettings")
                    DataCache.CacheClear(String.Format(CacheKeys.GroupInfo, ForumId))
                    DataCache.CacheClear(String.Format(CacheKeys.GroupInfo, ForumId) & "st")

                Case "deleteforum"
                    Dim ForumId As Integer = CInt(e.Parameters(1))
                    DataProvider.Instance.Forums_Delete(PortalId, ModuleId, ForumId)

                Case "deletegroup"
                    Dim GroupId As Integer = CInt(e.Parameters(1))
                    DataProvider.Instance.Groups_Delete(ModuleId, GroupId)

            End Select
            DataCache.CacheClear(String.Format(CacheKeys.ForumList, ModuleId))
            DataCache.ClearAllForumSettingsCache(ModuleId)
            DataCache.CacheClear(ModuleId & "fv")
           
            hidEditorResult.RenderControl(e.Output)
        End Sub


#End Region

#Region "Private Methods"
        Private Sub SaveSettings(sKey As String, Parameters() As String)
            DotNetNuke.Modules.ActiveForums.Settings.SaveSetting(ModuleId, sKey, ForumSettingKeys.TopicsTemplateId, Parameters(2).ToString)
            DotNetNuke.Modules.ActiveForums.Settings.SaveSetting(ModuleId, sKey, ForumSettingKeys.TopicTemplateId, Parameters(3).ToString)
            DotNetNuke.Modules.ActiveForums.Settings.SaveSetting(ModuleId, sKey, ForumSettingKeys.EmailAddress, Parameters(4).ToString)
            DotNetNuke.Modules.ActiveForums.Settings.SaveSetting(ModuleId, sKey, ForumSettingKeys.UseFilter, Parameters(5).ToString)
            DotNetNuke.Modules.ActiveForums.Settings.SaveSetting(ModuleId, sKey, ForumSettingKeys.AllowPostIcon, Parameters(6).ToString)
            DotNetNuke.Modules.ActiveForums.Settings.SaveSetting(ModuleId, sKey, ForumSettingKeys.AllowEmoticons, Parameters(7).ToString)
            DotNetNuke.Modules.ActiveForums.Settings.SaveSetting(ModuleId, sKey, ForumSettingKeys.AllowScript, Parameters(8).ToString)
            DotNetNuke.Modules.ActiveForums.Settings.SaveSetting(ModuleId, sKey, ForumSettingKeys.IndexContent, Parameters(9).ToString)
            DotNetNuke.Modules.ActiveForums.Settings.SaveSetting(ModuleId, sKey, ForumSettingKeys.AllowRSS, Parameters(10).ToString)
            DotNetNuke.Modules.ActiveForums.Settings.SaveSetting(ModuleId, sKey, ForumSettingKeys.AllowAttach, Parameters(11).ToString)
            DotNetNuke.Modules.ActiveForums.Settings.SaveSetting(ModuleId, sKey, ForumSettingKeys.AttachCount, Parameters(12).ToString)
            DotNetNuke.Modules.ActiveForums.Settings.SaveSetting(ModuleId, sKey, ForumSettingKeys.AttachMaxSize, Parameters(13).ToString)
            DotNetNuke.Modules.ActiveForums.Settings.SaveSetting(ModuleId, sKey, ForumSettingKeys.AttachTypeAllowed, Parameters(14).ToString)
            DotNetNuke.Modules.ActiveForums.Settings.SaveSetting(ModuleId, sKey, ForumSettingKeys.AttachStore, Parameters(15).ToString)
            DotNetNuke.Modules.ActiveForums.Settings.SaveSetting(ModuleId, sKey, ForumSettingKeys.AttachMaxHeight, Parameters(16).ToString)
            DotNetNuke.Modules.ActiveForums.Settings.SaveSetting(ModuleId, sKey, ForumSettingKeys.AttachMaxWidth, Parameters(17).ToString)
            DotNetNuke.Modules.ActiveForums.Settings.SaveSetting(ModuleId, sKey, ForumSettingKeys.AttachUniqueFileNames, Parameters(18).ToString)
            DotNetNuke.Modules.ActiveForums.Settings.SaveSetting(ModuleId, sKey, ForumSettingKeys.AllowHTML, Parameters(19).ToString)
            DotNetNuke.Modules.ActiveForums.Settings.SaveSetting(ModuleId, sKey, ForumSettingKeys.EditorType, CStr(IIf(CBool(Parameters(19)) = False, "0", Parameters(20).ToString)))
            DotNetNuke.Modules.ActiveForums.Settings.SaveSetting(ModuleId, sKey, ForumSettingKeys.EditorHeight, Parameters(21).ToString)
            DotNetNuke.Modules.ActiveForums.Settings.SaveSetting(ModuleId, sKey, ForumSettingKeys.EditorWidth, Parameters(22).ToString)
            DotNetNuke.Modules.ActiveForums.Settings.SaveSetting(ModuleId, sKey, ForumSettingKeys.EditorToolbar, Parameters(23).ToString)
            DotNetNuke.Modules.ActiveForums.Settings.SaveSetting(ModuleId, sKey, ForumSettingKeys.EditorStyle, Parameters(24).ToString)
            DotNetNuke.Modules.ActiveForums.Settings.SaveSetting(ModuleId, sKey, ForumSettingKeys.EditorPermittedUsers, Parameters(25).ToString)
            DotNetNuke.Modules.ActiveForums.Settings.SaveSetting(ModuleId, sKey, ForumSettingKeys.TopicFormId, Parameters(26).ToString)
            DotNetNuke.Modules.ActiveForums.Settings.SaveSetting(ModuleId, sKey, ForumSettingKeys.ReplyFormId, Parameters(27).ToString)
            DotNetNuke.Modules.ActiveForums.Settings.SaveSetting(ModuleId, sKey, ForumSettingKeys.QuickReplyFormId, Parameters(28).ToString)
            DotNetNuke.Modules.ActiveForums.Settings.SaveSetting(ModuleId, sKey, ForumSettingKeys.ProfileTemplateId, Parameters(29).ToString)
            DotNetNuke.Modules.ActiveForums.Settings.SaveSetting(ModuleId, sKey, ForumSettingKeys.IsModerated, Parameters(30).ToString)
            DotNetNuke.Modules.ActiveForums.Settings.SaveSetting(ModuleId, sKey, ForumSettingKeys.DefaultTrustValue, Parameters(31).ToString)
            DotNetNuke.Modules.ActiveForums.Settings.SaveSetting(ModuleId, sKey, ForumSettingKeys.AutoTrustLevel, Parameters(32).ToString)
            DotNetNuke.Modules.ActiveForums.Settings.SaveSetting(ModuleId, sKey, ForumSettingKeys.ModApproveTemplateId, Parameters(33).ToString)
            DotNetNuke.Modules.ActiveForums.Settings.SaveSetting(ModuleId, sKey, ForumSettingKeys.ModRejectTemplateId, Parameters(34).ToString)
            DotNetNuke.Modules.ActiveForums.Settings.SaveSetting(ModuleId, sKey, ForumSettingKeys.ModMoveTemplateId, Parameters(35).ToString)
            DotNetNuke.Modules.ActiveForums.Settings.SaveSetting(ModuleId, sKey, ForumSettingKeys.ModDeleteTemplateId, Parameters(36).ToString)
            DotNetNuke.Modules.ActiveForums.Settings.SaveSetting(ModuleId, sKey, ForumSettingKeys.ModNotifyTemplateId, Parameters(37).ToString)


            DotNetNuke.Modules.ActiveForums.Settings.SaveSetting(ModuleId, sKey, ForumSettingKeys.AutoSubscribeEnabled, Parameters(38).ToString)
            DotNetNuke.Modules.ActiveForums.Settings.SaveSetting(ModuleId, sKey, ForumSettingKeys.AutoSubscribeRoles, Parameters(39).ToString)
            DotNetNuke.Modules.ActiveForums.Settings.SaveSetting(ModuleId, sKey, ForumSettingKeys.AutoSubscribeNewTopicsOnly, Parameters(57).ToString)


            DotNetNuke.Modules.ActiveForums.Settings.SaveSetting(ModuleId, sKey, ForumSettingKeys.ActiveSocialEnabled, Parameters(58).ToString)
            DotNetNuke.Modules.ActiveForums.Settings.SaveSetting(ModuleId, sKey, ForumSettingKeys.ActiveSocialTopicsOnly, Parameters(59).ToString)
            DotNetNuke.Modules.ActiveForums.Settings.SaveSetting(ModuleId, sKey, ForumSettingKeys.ActiveSocialSecurityOption, Parameters(60).ToString)
        End Sub
        Private Sub LoadForum(ByVal ForumId As Integer)
            Dim fc As New ForumController
            Dim fi As Forum = fc.Forums_Get(ForumId, -1, False, False)
            Dim NewForum As Forum = fc.GetForum(PortalId, ModuleId, ForumId, True)
            ctlSecurityGrid = CType(LoadControl("~/DesktopModules/activeforums/controls/admin_securitygrid.ascx"), Controls.admin_securitygrid)
            ctlSecurityGrid.Perms = NewForum.Security
            ctlSecurityGrid.PermissionsId = NewForum.PermissionsId
            ctlSecurityGrid.ModuleConfiguration = Me.ModuleConfiguration
            ctlSecurityGrid.ReadOnly = NewForum.InheritSecurity

            plhGrid.Controls.Clear()
            plhGrid.Controls.Add(ctlSecurityGrid)
            If Not fi Is Nothing Then
                txtForumName.Text = fi.ForumName
                txtForumDesc.Text = fi.ForumDesc
                chkActive.Checked = fi.Active
                chkHidden.Checked = fi.Hidden
                hidForumId.Value = fi.ForumID.ToString
                txtPrefixURL.Text = fi.PrefixURL
                Dim iIndex As Integer
                If fi.ParentForumId > 0 Then
                    iIndex = drpGroups.Items.IndexOf(drpGroups.Items.FindByValue("FORUM" & CType(fi.ParentForumId, String)))
                Else
                    iIndex = drpGroups.Items.IndexOf(drpGroups.Items.FindByValue("GROUP" & CType(fi.ForumGroupId, String)))
                End If
                drpGroups.SelectedIndex = iIndex
                If fi.ForumSettingsKey = "G:" & fi.ForumGroupId.ToString Then
                    chkInheritGroup.Checked = True
                    trTemplates.Attributes.Add("style", "display:none;")
                End If
                drpTopicsTemplate.SelectedIndex = drpTopicsTemplate.Items.IndexOf(drpTopicsTemplate.Items.FindByValue(fi.TopicsTemplateId.ToString))
                drpTopicTemplate.SelectedIndex = drpTopicTemplate.Items.IndexOf(drpTopicTemplate.Items.FindByValue(fi.TopicTemplateId.ToString))
                drpTopicForm.SelectedIndex = drpTopicForm.Items.IndexOf(drpTopicForm.Items.FindByValue(fi.TopicFormId.ToString))
                drpReplyForm.SelectedIndex = drpReplyForm.Items.IndexOf(drpReplyForm.Items.FindByValue(fi.ReplyFormId.ToString))
                'drpQuickReplyForm.SelectedIndex = drpQuickReplyForm.Items.IndexOf(drpQuickReplyForm.Items.FindByValue(fi.QuickReplyFormId.ToString))
                drpProfileDisplay.SelectedIndex = drpProfileDisplay.Items.IndexOf(drpProfileDisplay.Items.FindByValue(fi.ProfileTemplateId.ToString))
                drpModApprovedTemplateId.SelectedIndex = drpModApprovedTemplateId.Items.IndexOf(drpModApprovedTemplateId.Items.FindByValue(fi.ModApproveTemplateId.ToString))
                drpModRejectTemplateId.SelectedIndex = drpModRejectTemplateId.Items.IndexOf(drpModRejectTemplateId.Items.FindByValue(fi.ModRejectTemplateId.ToString))
                drpModDeleteTemplateId.SelectedIndex = drpModDeleteTemplateId.Items.IndexOf(drpModDeleteTemplateId.Items.FindByValue(fi.ModDeleteTemplateId.ToString))
                drpModMoveTemplateId.SelectedIndex = drpModMoveTemplateId.Items.IndexOf(drpModMoveTemplateId.Items.FindByValue(fi.ModMoveTemplateId.ToString))
                drpModNotifyTemplateId.SelectedIndex = drpModNotifyTemplateId.Items.IndexOf(drpModNotifyTemplateId.Items.FindByValue(fi.ModNotifyTemplateId.ToString))
                drpDefaultTrust.SelectedIndex = drpDefaultTrust.Items.IndexOf(drpDefaultTrust.Items.FindByValue(CInt(fi.DefaultTrustValue).ToString))
                txtAutoTrustLevel.Text = fi.AutoTrustLevel.ToString
                txtEmailAddress.Text = fi.EmailAddress
                If fi.UseFilter Then
                    rdFilterOn.Checked = True
                    rdFilterOff.Checked = False
                Else
                    rdFilterOn.Checked = False
                    rdFilterOff.Checked = True
                End If
                If fi.AllowPostIcon Then
                    rdPostIconOn.Checked = True
                    rdPostIconOff.Checked = False
                Else
                    rdPostIconOn.Checked = False
                    rdPostIconOff.Checked = True
                End If
                If fi.AllowEmoticons Then
                    rdEmotOn.Checked = True
                    rdEmotOff.Checked = False
                Else
                    rdEmotOn.Checked = False
                    rdEmotOff.Checked = True
                End If
                If fi.AllowScript Then
                    rdScriptsOn.Checked = True
                    rdScriptsOff.Checked = False
                Else
                    rdScriptsOn.Checked = False
                    rdScriptsOff.Checked = True
                End If

              


                If fi.IndexContent Then
                    rdIndexOn.Checked = True
                    rdIndexOff.Checked = False
                Else
                    rdIndexOn.Checked = False
                    rdIndexOff.Checked = True
                End If
                If fi.AllowRSS Then
                    rdRSSOn.Checked = True
                    rdRSSOff.Checked = False
                Else
                    rdRSSOn.Checked = False
                    rdRSSOff.Checked = True
                End If
                If fi.AllowAttach Then
                    rdAttachOn.Checked = True
                    rdAttachOff.Checked = False
                Else
                    rdAttachOn.Checked = False
                    rdAttachOff.Checked = True
                End If
                If fi.AllowHTML Then
                    rdHTMLOn.Checked = True
                    rdHTMLOff.Checked = False
                    cfgHTML.Attributes.Remove("style")
                Else
                    rdHTMLOn.Checked = False
                    rdHTMLOff.Checked = True
                    cfgHTML.Attributes.Add("style", "display:none;")
                End If
                If fi.IsModerated Then
                    rdModOn.Checked = True
                    rdModOff.Checked = False
                    cfgMod.Attributes.Remove("style")
                Else
                    rdModOn.Checked = False
                    rdModOff.Checked = True
                    cfgMod.Attributes.Add("style", "display:none;")
                End If

                If fi.AutoSubscribeEnabled Then
                    rdAutoSubOn.Checked = True
                    rdAutoSubOff.Checked = False
                    cfgAutoSub.Attributes.Remove("style")
                Else
                    rdAutoSubOn.Checked = False
                    rdAutoSubOff.Checked = True
                    cfgAutoSub.Attributes.Add("style", "display:none;")
                End If
                chkTopicsOnly.Checked = fi.AutoSubscribeNewTopicsOnly

                drpEditorTypes.SelectedIndex = drpEditorTypes.Items.IndexOf(drpEditorTypes.Items.FindByValue(CInt(fi.EditorType).ToString))
                If CInt(fi.EditorType).ToString <> "1" Then
                    txtEditorToolBar.Enabled = False
                    drpEditorStyle.Enabled = False
                End If


                drpEditorStyle.SelectedIndex = drpEditorStyle.Items.IndexOf(drpEditorStyle.Items.FindByValue(CStr(fi.EditorStyle)))
                drpPermittedRoles.SelectedIndex = drpPermittedRoles.Items.IndexOf(drpPermittedRoles.Items.FindByValue(CInt(fi.EditorPermittedUsers).ToString))
              

                txtEditorHeight.Text = CStr(IIf(fi.EditorHeight = String.Empty, "400", fi.EditorHeight))
                txtEditorWidth.Text = CStr(IIf(fi.EditorWidth = String.Empty, "99%", fi.EditorWidth))
                txtEditorToolBar.Text = CStr(IIf(fi.EditorToolBar = String.Empty, "bold,italic,underline|bulletedlist,numberedlist", fi.EditorToolBar))

                hidRoles.Value = fi.AutoSubscribeRoles
                BindAutoSubRoles(fi.AutoSubscribeRoles)



            End If
        End Sub
        Private Sub LoadGroup(ByVal GroupId As Integer)
            Dim gc As New ForumGroupController
            Dim gi As ForumGroupInfo = gc.Groups_Get(ModuleId, GroupId)
            Dim newGroup As ForumGroupInfo = gc.GetForumGroup(ModuleId, GroupId)
            ctlSecurityGrid = CType(LoadControl("~/DesktopModules/activeforums/controls/admin_securitygrid.ascx"), Controls.admin_securitygrid)
            ctlSecurityGrid.Perms = newGroup.Security
            ctlSecurityGrid.PermissionsId = newGroup.PermissionsId
            ctlSecurityGrid.ModuleConfiguration = Me.ModuleConfiguration
            plhGrid.Controls.Clear()
            plhGrid.Controls.Add(ctlSecurityGrid)
            If Not gi Is Nothing Then
                With gi
                    trGroups.Visible = False
                    reqGroups.Enabled = False
                    txtForumName.Text = .GroupName
                    chkActive.Checked = .Active
                    chkHidden.Checked = .Hidden
                    hidForumId.Value = .ForumGroupId.ToString
                    hidSortOrder.Value = .SortOrder.ToString
                    txtPrefixURL.Text = .PrefixURL
                End With

                drpTopicsTemplate.SelectedIndex = drpTopicsTemplate.Items.IndexOf(drpTopicsTemplate.Items.FindByValue(gi.TopicsTemplateId.ToString))
                drpTopicTemplate.SelectedIndex = drpTopicTemplate.Items.IndexOf(drpTopicTemplate.Items.FindByValue(gi.TopicTemplateId.ToString))
                drpTopicForm.SelectedIndex = drpTopicForm.Items.IndexOf(drpTopicForm.Items.FindByValue(gi.TopicFormId.ToString))
                drpReplyForm.SelectedIndex = drpReplyForm.Items.IndexOf(drpReplyForm.Items.FindByValue(gi.ReplyFormId.ToString))
                'drpQuickReplyForm.SelectedIndex = drpQuickReplyForm.Items.IndexOf(drpQuickReplyForm.Items.FindByValue(gi.QuickReplyFormId.ToString))
                drpProfileDisplay.SelectedIndex = drpProfileDisplay.Items.IndexOf(drpProfileDisplay.Items.FindByValue(gi.ProfileTemplateId.ToString))
                drpModApprovedTemplateId.SelectedIndex = drpModApprovedTemplateId.Items.IndexOf(drpModApprovedTemplateId.Items.FindByValue(gi.ModApproveTemplateId.ToString))
                drpModRejectTemplateId.SelectedIndex = drpModRejectTemplateId.Items.IndexOf(drpModRejectTemplateId.Items.FindByValue(gi.ModRejectTemplateId.ToString))
                drpModDeleteTemplateId.SelectedIndex = drpModDeleteTemplateId.Items.IndexOf(drpModDeleteTemplateId.Items.FindByValue(gi.ModDeleteTemplateId.ToString))
                drpModMoveTemplateId.SelectedIndex = drpModMoveTemplateId.Items.IndexOf(drpModMoveTemplateId.Items.FindByValue(gi.ModMoveTemplateId.ToString))
                drpModNotifyTemplateId.SelectedIndex = drpModNotifyTemplateId.Items.IndexOf(drpModNotifyTemplateId.Items.FindByValue(gi.ModNotifyTemplateId.ToString))
                drpDefaultTrust.SelectedIndex = drpDefaultTrust.Items.IndexOf(drpDefaultTrust.Items.FindByValue(gi.DefaultTrustValue.ToString))
                txtAutoTrustLevel.Text = gi.AutoTrustLevel.ToString
                txtEmailAddress.Text = gi.EmailAddress
                If gi.UseFilter Then
                    rdFilterOn.Checked = True
                    rdFilterOff.Checked = False
                Else
                    rdFilterOn.Checked = False
                    rdFilterOff.Checked = True
                End If
                If gi.AllowPostIcon Then
                    rdPostIconOn.Checked = True
                    rdPostIconOff.Checked = False
                Else
                    rdPostIconOn.Checked = False
                    rdPostIconOff.Checked = True
                End If
                If gi.AllowEmoticons Then
                    rdEmotOn.Checked = True
                    rdEmotOff.Checked = False
                Else
                    rdEmotOn.Checked = False
                    rdEmotOff.Checked = True
                End If
                If gi.AllowScript Then
                    rdScriptsOn.Checked = True
                    rdScriptsOff.Checked = False
                Else
                    rdScriptsOn.Checked = False
                    rdScriptsOff.Checked = True
                End If
                If gi.IndexContent Then
                    rdIndexOn.Checked = True
                    rdIndexOff.Checked = False
                Else
                    rdIndexOn.Checked = False
                    rdIndexOff.Checked = True
                End If
                If gi.AllowRSS Then
                    rdRSSOn.Checked = True
                    rdRSSOff.Checked = False
                Else
                    rdRSSOn.Checked = False
                    rdRSSOff.Checked = True
                End If

                If gi.AllowAttach Then
                    rdAttachOn.Checked = True
                    rdAttachOff.Checked = False
                Else
                    rdAttachOn.Checked = False
                    rdAttachOff.Checked = True
                End If
                If gi.AllowHTML Then
                    rdHTMLOn.Checked = True
                    rdHTMLOff.Checked = False
                    cfgHTML.Attributes.Remove("style")
                Else
                    rdHTMLOn.Checked = False
                    rdHTMLOff.Checked = True
                    cfgHTML.Attributes.Add("style", "display:none;")
                End If
                If gi.IsModerated Then
                    rdModOn.Checked = True
                    rdModOff.Checked = False
                    cfgMod.Attributes.Remove("style")
                Else
                    rdModOn.Checked = False
                    rdModOff.Checked = True
                    cfgMod.Attributes.Add("style", "display:none;")
                End If
                If gi.AutoSubscribeEnabled Then
                    rdAutoSubOn.Checked = True
                    rdAutoSubOff.Checked = False
                    cfgAutoSub.Attributes.Remove("style")
                Else
                    rdAutoSubOn.Checked = False
                    rdAutoSubOff.Checked = True
                    cfgAutoSub.Attributes.Add("style", "display:none;")
                End If
                drpEditorTypes.SelectedIndex = drpEditorTypes.Items.IndexOf(drpEditorTypes.Items.FindByValue(CInt(gi.EditorType).ToString))
                drpEditorStyle.SelectedIndex = drpEditorStyle.Items.IndexOf(drpEditorStyle.Items.FindByValue(CStr(gi.EditorStyle)))
                drpPermittedRoles.SelectedIndex = drpPermittedRoles.Items.IndexOf(drpPermittedRoles.Items.FindByValue(CInt(gi.EditorPermittedUsers).ToString))
                

                txtEditorHeight.Text = CStr(IIf(gi.EditorHeight = String.Empty, "400", gi.EditorHeight))
                txtEditorWidth.Text = CStr(IIf(gi.EditorWidth = String.Empty, "99%", gi.EditorWidth))
                txtEditorToolBar.Text = CStr(IIf(gi.EditorToolBar = String.Empty, "bold,italic,underline|bulletedlist,numberedlist", gi.EditorToolBar))
                chkTopicsOnly.Checked = gi.AutoSubscribeNewTopicsOnly
                hidRoles.Value = gi.AutoSubscribeRoles
                BindAutoSubRoles(gi.AutoSubscribeRoles)
            End If
        End Sub
        Private Sub BindAutoSubRoles(ByVal Roles As String)
            Dim sb As New Text.StringBuilder
            sb.Append("<table id=""tblRoles"" cellpadding=""0"" cellspacing=""0"" width=""99%"">")
            sb.Append("<tr><td width=""99%""></td><td></td></tr>")
            If Not Roles Is Nothing Then
                Dim rc As New DotNetNuke.Security.Roles.RoleController
                Dim ri As DotNetNuke.Security.Roles.RoleInfo
                Dim arr As ArrayList = rc.GetPortalRoles(PortalId)
                For Each ri In arr
                    Dim sRoles() As String = Roles.Split(CChar(";"))
                    Dim role As String = String.Empty
                    For Each role In sRoles
                        If role = ri.RoleID.ToString Then
                            sb.Append("<tr><td class=""amcpnormal"">" & ri.RoleName & "</td><td><img src=""" & Page.ResolveUrl("~/DesktopModules/ActiveForums/images/delete16.png") & """ onclick=""removeRole(this," & ri.RoleID & ");"" /></td></tr>")
                        End If
                    Next
                Next
            End If
            sb.Append("</table>")
            tbRoles.Text = sb.ToString
        End Sub
        Private Sub BindGroups()
            Dim dr As IDataReader = DataProvider.Instance.Forums_List(PortalId, ModuleId, -1, -1, False)
            drpGroups.Items.Add(New ListItem(Utilities.GetSharedResource("DropDownSelect"), "-1"))
            Dim tmpGroupId As Integer = -1
            While dr.Read
                If Not tmpGroupId = CInt(dr("ForumGroupId")) Then
                    drpGroups.Items.Add(New ListItem(dr("GroupName").ToString, "GROUP" & dr("ForumGroupId").ToString))
                    tmpGroupId = CInt(dr("ForumGroupId"))
                End If
                If Not CInt(dr("ForumId")) = 0 Then
                    If CInt(dr("ParentForumID")) = 0 Then
                        drpGroups.Items.Add(New ListItem(" - " & dr("ForumName").ToString, "FORUM" & dr("ForumId").ToString))
                    End If
                End If
            End While
            dr.Close()
        End Sub
        Private Sub BindTabs()
            Dim sb As New StringBuilder
            Dim sLabel As String = "[RESX:Group]"
            If editorType = "F" Then
                sLabel = "[RESX:Forum]"
            End If
            sb.Append("<div id=""divForum"" onclick=""toggleTab(this);"" class=""amtabsel"" style=""margin-left:10px;""><div id=""divForum_text"" class=""amtabseltext"">" & sLabel & "</div></div>")
            If recordId > 0 Then
                sb.Append("<div id=""divSecurity"" onclick=""toggleTab(this);"" class=""amtab""><div id=""divSecurity_text"" class=""amtabtext"">[RESX:Security]</div></div>")
                If editorType = "F" And chkInheritGroup.Checked Then
                    sb.Append("<div id=""divSettings"" onclick=""toggleTab(this);"" class=""amtab"" style=""display:none;""><div id=""divSettings_text"" class=""amtabtext"">[RESX:Features]</div></div>")
                Else
                    sb.Append("<div id=""divSettings"" onclick=""toggleTab(this);"" class=""amtab""><div id=""divSettings_text"" class=""amtabtext"">[RESX:Features]</div></div>")
                End If
                If editorType = "F" Then
                    sb.Append("<div id=""divClean"" onclick=""toggleTab(this);"" class=""amtab""><div id=""divClean_text"" class=""amtabtext"">[RESX:Maintenance]</div></div>")
                    sb.Append("<div id=""divProperties"" onclick=""toggleTab(this);"" class=""amtab""><div id=""divProperties_text"" class=""amtabtext"">[RESX:TopicProperties]</div></div>")
                End If

            End If
            litTabs.Text = sb.ToString
        End Sub
        Private Sub BindRoles()
            Dim rc As New DotNetNuke.Security.Roles.RoleController
            drpRoles.DataTextField = "RoleName"
            drpRoles.DataValueField = "RoleId"
            drpRoles.DataSource = rc.GetPortalRoles(PortalId)
            drpRoles.DataBind()
            drpRoles.Items.Insert(0, New ListItem("[RESX:DropDownDefault]", ""))
        End Sub



        Private Sub BindTemplates()
            BindTemplateDropDown(drpTopicsTemplate, Templates.TemplateTypes.TopicsView, "[RESX:Default]", "0")
            BindTemplateDropDown(drpTopicTemplate, Templates.TemplateTypes.TopicView, "[RESX:Default]", "0")
            BindTemplateDropDown(drpTopicForm, Templates.TemplateTypes.TopicForm, "[RESX:Default]", "0")
            BindTemplateDropDown(drpReplyForm, Templates.TemplateTypes.ReplyForm, "[RESX:Default]", "0")
            'BindTemplateDropDown(drpQuickReplyForm, Templates.TemplateTypes.QuickReplyForm, "[RESX:Default]", "0")
            BindTemplateDropDown(drpProfileDisplay, Templates.TemplateTypes.PostInfo, "[RESX:Default]", "0")

            BindTemplateDropDown(drpModApprovedTemplateId, Templates.TemplateTypes.ModEmail, "[RESX:DropDownDisabled]", "-1")
            BindTemplateDropDown(drpModDeleteTemplateId, Templates.TemplateTypes.ModEmail, "[RESX:DropDownDisabled]", "-1")
            BindTemplateDropDown(drpModMoveTemplateId, Templates.TemplateTypes.ModEmail, "[RESX:DropDownDisabled]", "-1")
            BindTemplateDropDown(drpModRejectTemplateId, Templates.TemplateTypes.ModEmail, "[RESX:DropDownDisabled]", "-1")
            BindTemplateDropDown(drpModNotifyTemplateId, Templates.TemplateTypes.ModEmail, "[RESX:DropDownDisabled]", "-1")


        End Sub


#End Region


        Private Sub cbMaint_Callback(ByVal sender As Object, ByVal e As Controls.CallBackEventArgs) Handles cbMaint.Callback
            Try
                Dim fid As Integer = CInt(e.Parameters(1))
                Dim olderThan As Integer = CInt(e.Parameters(2))
                Dim byUserId As Integer = CInt(e.Parameters(3))
                Dim lastActive As Integer = CInt(e.Parameters(4))
                Dim withNotReplies As Boolean = CBool(e.Parameters(5))
                Dim dryRun As Boolean = CBool(e.Parameters(6))
                Dim rows As Integer = DataProvider.Instance.Forum_Maintenance(fid, olderThan, lastActive, byUserId, withNotReplies, dryRun, MainSettings.DeleteBehavior)
                If dryRun Then
                    litMaintStatus.Text = String.Format(GetSharedResource("[RESX:Maint:DryRunResults]"), rows.ToString)
                Else
                    litMaintStatus.Text = "<div class=""afsuccess"">" & GetSharedResource("[RESX:ProcessComplete]") & "</div>"
                End If

            Catch ex As Exception
                litMaintStatus.Text = "<div class=""aferror"">" & ex.Message & "</div>"
            End Try

            litMaintStatus.RenderControl(e.Output)
        End Sub



    End Class
End Namespace
